<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>欢迎来到登陆界面</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <style>
    html{
        background: url(images/11.png) no-repeat;
        background-size: 100% 100%;
        min-width: 1250px;
        }
    form{
        margin: 50px 540px;
    }
    </style>
</head>
<body>
    <div class="h1 text-center page-header">
        <a style="text-decoration: none" href="#" title="京东">
            <h1> <img src="images/3.png" alt="">
            <small>欢迎登陆</small></h1>
            </a>         
    </div>
    <form>
        <div class="form-group">
            <label for="exampleInputEmail1">账号</label>
            <input type="email" class="form-control" id="usename" placeholder="请输入账号"><span></span>
        </div>
        <div class="form-group">
            <label for="exampleInputPassword1">密码</label>
             <input type="password" class="form-control" id="password" placeholder="请输入密码"><span></span>
        </div>
        <div class="form-group">
            <label for="exampleInputPassword1">确认密码</label>
            <input type="password" class="form-control" id="repassword" placeholder="请再次输入密码"><span></span>
         </div>
         <div class="checkbox">
            <label>
                <input type="checkbox" id="rem"> 是否记住用户名
            </label>
        </div>
        <button type="button" class="btn btn-default">登陆</button>
        <button type="button" style="margin-left: 60px" class="btn btn-default zc">立即注册</button>
    </form>
</body>
<script src="js/promiseAjax.js"></script>
<script src="js/cookie.js"></script>
<script>
    var z=document.querySelector("#usename");
    var x=getCookie("uname");
    if(x){
        z.value=x;
    }else{
        z.value='';
    }

    var zc=document.querySelector(".zc");
    zc.onclick=function(){
        location.href='reg.html'
    }
    var button=document.querySelector("button[type='button']");
    button.addEventListener("click",login);
    function login(){
        var usename=document.querySelector("#usename").value.trim();
        var password=document.querySelector("#password").value.trim();
        var rem=document.querySelector("#rem");
        pAjax({
            url:"php/login.php",
            type:"post",
            data:{
                usename:usename,
                password:password,
            },
        }).then(function(res){
            if(res.status==200){
                setCookie("usename",usename,7200);
                if(rem.checked){
                    setCookie("uname",usename,3600*24*7);
                }else{
                    removeCookie("uname");
                }
                alert(res.msg);
                location.href='home.html';
            }else{
                alert(res.msg);
                return;
            }
        })
    }

</script>
</html>